<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <!--<el-form-item label="父Id" prop="parentId">-->
        <!--<el-input-->
          <!--v-model="queryParams.parentId"-->
          <!--placeholder="请输入父Id"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="所属园区" prop="parkNum">
        <el-select v-model="queryParams.parkNum" placeholder="请选择园区">
          <el-option
            v-for="item in parkList"
            :key="item.id"
            :label="item.parkNum"
            :value="item.parkNum"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="仪表编码" prop="meterNum">
        <el-input
          v-model="queryParams.meterNum"
          placeholder="请输入仪表编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="安装位置" prop="installPosition">-->
        <!--<el-input-->
          <!--v-model="queryParams.installPosition"-->
          <!--placeholder="请输入安装位置"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="仪表类别" prop="category">
        <el-select
          v-model="queryParams.category"
          placeholder="请选择仪表类别(总表,私表..)"
          clearable>
          <el-option
            v-for="item in categoryOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="用途" prop="use1">-->
        <!--<el-select-->
          <!--v-model="queryParams.use1"-->
          <!--placeholder="请选择仪表用途"-->
          <!--clearable>-->
          <!--<el-option-->
            <!--v-for="item in meterUseTypeList"-->
            <!--:key="item.dictValue"-->
            <!--:label="item.dictLabel"-->
            <!--:value="item.dictValue"-->
          <!--&gt;-->
          <!--</el-option>-->
        <!--</el-select>-->
      <!--</el-form-item>-->

      <!--<el-form-item label="用户名称" prop="enterpriseName">-->
        <!--<el-input-->
          <!--v-model="queryParams.enterpriseName"-->
          <!--placeholder="请输入企业名称"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['energy:nyMeterAccount2:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['energy:nyMeterAccount2:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['energy:nyMeterAccount2:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleImport"
          v-hasPermi="['energy:nyMeterAccount2:edit']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['energy:nyMeterAccount2:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="nyMeterAccount2List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="Id" align="center" prop="id" />-->
      <!--<el-table-column label="父Id" align="center" prop="parentId" />-->
      <el-table-column label="仪表编码" align="center" prop="meterNum" />
      <el-table-column label="上级编码" align="center" prop="parentNum" />
      <el-table-column label="仪表类别" align="center" prop="category" />
      <!--<el-table-column label="园区编号" align="center" prop="parkNum" />-->
      <el-table-column label="编号" align="center" prop="meterModel" />
      <el-table-column label="安装位置" align="center" prop="installPosition" />
      <el-table-column label="用途" align="center" prop="use1" />
      <el-table-column label="用户" align="center" prop="enterpriseName" />
      <el-table-column label="分摊池值" align="center" prop="shareInitialValue" />
      <el-table-column label="当前读数" align="center" prop="currentValue" />
      <el-table-column label="当前读日期" align="center" prop="currentCheckDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.currentCheckDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['energy:nyMeterAccount2:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['energy:nyMeterAccount2:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仪台账运营对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仪表编码" prop="id" v-if="form.isAdd == '1'">
          <el-select clearable v-model="form.id" placeholder="请选择仪表" @change="selectPark(form.id)" >
            <el-option
              v-for="item in nyMeterAccount1List"
              :key="item.id"
              :label="item.meterNumNew"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!--下拉列表，总表考核表-->
        <el-form-item label="上级编码" prop="parentId">
          <el-select clearable v-model="form.parentId" placeholder="请选择上级仪表">
            <el-option
              v-for="item in nyMeterAccount1ListParent"
              :key="item.id"
              :label="item.meterNumNew"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="仪表类别" prop="category">
          <el-select v-model="form.category" placeholder="请选择仪表类别(总表,私表..)" clearable>
            <el-option
              v-for="item in categoryOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <!--<el-form-item label="房源Id" prop="roomId">-->
          <!--<el-input v-model="form.roomId" placeholder="请输入房源Id" />-->
        <!--</el-form-item>-->
        <el-form-item label="楼栋编号" prop="buildingNum">
          <el-input v-model="form.buildingNum" placeholder="请输入楼栋编号" />
        </el-form-item>
        <!--<el-form-item label="楼栋" prop="buildingNum">-->
          <!--<el-select v-model="form.buildingNum" placeholder="请选择楼栋">-->
            <!--<el-option-->
              <!--v-for="item in buildingList"-->
              <!--:key="item.id"-->
              <!--:label="item.buildingNum"-->
              <!--:value="item.buildingNum"-->
            <!--/>-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="园区编号" prop="parkNum">-->
          <!--<el-input v-model="form.parkNum" placeholder="请输入园区编号" />-->
        <!--</el-form-item>-->
        <el-form-item label="安装位置" prop="installPosition">
          <el-input v-model="form.installPosition" placeholder="请输入安装位置" />
        </el-form-item>
        <!--<el-form-item label="企业编号" prop="enterpriseId">-->
          <!--<el-input v-model="form.enterpriseId" placeholder="请输入企业编号" />-->
        <!--</el-form-item>-->
        <el-form-item label="用途" prop="use1">
          <el-select clearable v-model="form.use1" placeholder="请选择仪表用途">
            <el-option
              v-for="item in meterUseTypeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="form.enterpriseName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="分摊池值" prop="shareInitialValue">
          <el-input v-model="form.shareInitialValue" placeholder="请输入分摊池值" />
        </el-form-item>
        <el-form-item label="当前读数" prop="currentValue">
          <el-input v-model="form.currentValue" placeholder="请输入当前读数" />
        </el-form-item>
        <el-form-item label="当前读日期" prop="currentCheckDate">
          <el-date-picker clearable size="small"
            v-model="form.currentCheckDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择当前读日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 抄表导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?meterType=' + upload.meterType"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <!--<el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据-->
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listNyMeterAccount2, getNyMeterAccount2, delNyMeterAccount2, addNyMeterAccount2, updateNyMeterAccount2 } from "@/api/smartParks/energy/nyMeterAccount2";
import { listNyMeterAccount1} from "@/api/smartParks/energy/nyMeterAccount1";
import { listData } from "@/api/system/dict/data"; //字典接口
import { getToken } from "@/utils/auth";
import {listBuiPark} from "@/api/parkAssets/enterCommunity";
import {listBuiBuilding} from "@/api/parkAssets/building";
import * as request from '@/utils/request';

export default {
  name: "NyMeterAccount2",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仪台账运营表格数据
      nyMeterAccount2List: [],
      // 仪台账基础表格数据
      nyMeterAccount1List: [],
      meterTypeList: [],
      meterUseTypeList: [],
      workState: [],
      parkList: [],
      categoryOptions:[],
      buildingList: [],
      nyMeterAccount1ListParent: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: null,
        category: null,
        use1: null,
        roomId: null,
        buildingNum: null,
        parkNum: null,
        parkId: null,
        installPosition: null,
        enterpriseId: null,
        enterpriseName: null,
        shareInitialValue: null,
        currentValue: null,
        currentCheckDate: null,
        meterType:'电表'
      },
      // 表单参数
      form: {isAdd:'1'},
      // 用户导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        meterType: '电表',
        // 设置上传的请求头部
        headers: { Authorization: getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/energy/nyMeterAccount2/importData"
      },
      // 表单校验
      rules: {
        id:[{required: true,message: "仪表编码不能为空", trigger: "change",}],
        category:[{required: true,message: "仪表类别不能为空", trigger: "change",}],
        shareInitialValue:[{required: true,message: "分摊池不能为空", trigger: "change",}],
      }
    };
  },
  created() {
    this.getList();
    this.getListFirst();
  },
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "抄表导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },

    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('energy/nyMeterAccount2/importTemplate',{
        ...this.queryParams
      }, `水表导入模板.xlsx`)
    },
    genCode(item){
      this.form.sysBmId=item.bmId
    },
    /** 查询仪台账运营列表 */
    getList() {
      this.loading = true;
      listNyMeterAccount2(this.queryParams).then(response => {
        // response.rows.forEach((item) => {
        //   item.categoryNew = this.selectDictLabel(this.categoryOptions, item.category);
        //   this.workState.forEach((itemType) => {
        //     if (itemType.dictValue === item.state) {
        //       item.stateNew = itemType.dictLabel;
        //     }
        //   });
        // });
        this.nyMeterAccount2List = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListFirst() {
      listData({ dictType: "meter_type" }).then((response) => {
        this.meterTypeList = response.rows;
      });
      listData({ dictType: "mater_use_type" }).then((response) => {
        this.meterUseTypeList = response.rows;
      });
      listData({ dictType: "device_work_state" }).then((response) => {
        this.workState = response.rows;
      });
      this.getDicts("meter_category").then(response => {
        this.categoryOptions = response.data;
      });
      listNyMeterAccount1({params:{diff:"1"}}).then(response => {
        response.rows.forEach((item) => {
          item.meterNumNew = item.meterNum + "==" + item.meterName +"=="+ item.meterModel;
        });
        this.nyMeterAccount1List = response.rows;
      });
      listNyMeterAccount1().then(response => {
        response.rows.forEach((item) => {
          item.meterNumNew = item.meterNum + "==" + item.meterName +"=="+ item.meterModel;
        });
        this.nyMeterAccount1ListParent = response.rows;
      });
      listBuiPark().then((response) =>{
        this.parkList = response.rows;
      });

      // listQyEnterprise().then((response) => {
      //   this.qyEnterpriseList = response.rows;
      // });
      // listWySupplier().then((response) => {
      //   this.supplierList = response.rows;
      // })
    },
    selectPark(value){
      //buildingList,parkList
      //this.form.buildingNum = "";
      this.$set(this.form, "buildingNum", "");
      this.buildingList=[];
      let obj = {};
      obj = this.nyMeterAccount1ListParent.find((item)=>{
        return item.id === value;
      });
      listBuiBuilding({ parkNum: obj.parkNum }).then((response) =>{
        this.buildingList = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        category: null,
        use1: null,
        roomId: null,
        buildingNum: null,
        parkNum: null,
        parkId: null,
        installPosition: null,
        enterpriseId: null,
        enterpriseName: null,
        shareInitialValue: null,
        currentValue: null,
        currentCheckDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.isAdd ='1';
      this.title = "添加电表运营";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNyMeterAccount2(id).then(response => {
        this.form = response.data;
        this.form.isAdd ='0';
        this.open = true;
        this.title = "修改仪台账运营";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.isAdd == '0') {
            updateNyMeterAccount2(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNyMeterAccount2(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除仪台账运营编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delNyMeterAccount2(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      request.downloadCheckMeter1('energy/nyMeterAccount2/export', {
        ...this.queryParams
      }, `电表运营数据.xlsx`)
    }
  }
};
</script>
